rm(list = ls())
# devtools::install_github("rstats-db/hms")
# devtools::install_github("tidyverse/tibble")
setwd("C:/Users/Jon/Dropbox/BES backup/social media paper/replication package")
# Note that the haven package has some bugs which the
# following script works around
# install.packages("haven")
library(haven)

bes <- read_dta("data/bes_f2f_using.dta")

#### Read and format #### 
# labelDataset <- function(data) {
#   correctLabel <- function(x) {
#     if(!is.null(attributes(x)$labels)) {
#       class(attributes(x)$labels) <- typeof(x)
#     }
#     return(x)
#   }
#   for(i in colnames(data)) {
#     data[, i] <- correctLabel(data[, i])
#   }
#   return(data)
# }

bes <- labelDataset(bes)
bes$Age[bes$Age < 18] <- NA

bes$twitterUse <- as.character(bes$twitterUse)
bes$twitterUse[bes$twitterUse=="0"] <- "No"
bes$twitterUse[bes$twitterUse=="1"] <- "Yes"
bes$twitterUse[bes$twitterUse=="Don't know"] <- NA
bes$fbUse <- as.character(bes$fbUse)
bes$fbUse[bes$fbUse=="0"] <- "No"
bes$fbUse[bes$fbUse=="1"] <- "Yes"
bes$fbUse[bes$fbUse=="Don't know"] <- NA

bes$Age <- as.numeric(bes$Age)

bes.2 <- bes
bes.2$twitterUse <- "Population"

bes.2$fbUse <- "Population"
bes.1 <- bes
bes <- rbind(bes.1, bes.2)
bes$twitterUse[which(bes$twitterUse=="Yes")] <- "Twitter user"
bes$twitterUse[which(bes$twitterUse=="No")] <- "Non-user"
bes$twitterUse <- factor(bes$twitterUse, levels = c("Population",
                                                    "Twitter user", "Non-user"))

bes$fbUse[which(bes$fbUse=="Yes")] <- "Facebook user"
bes$fbUse[which(bes$fbUse=="No")] <- "Non-user"
bes$fbUse <- factor(bes$fbUse, levels = c("Population",
                                          "Facebook user", "Non-user"))

bes$polAttention[bes$polAttention=="Don't know"] <- NA

bes$Group <- bes$twitterUse
bes$gender <- as.character(bes$gender)
bes$gender[bes$gender=="1"] <- "Female"
bes$gender[bes$gender=="0"] <- "Male"

ed.names <- names(attributes(bes$edlevel)$labels)
bes$edlevel <- ed.names[match(bes$edlevel, attributes(bes$edlevel)$labels)]
bes$edlevel <- factor(bes$edlevel, levels = ed.names)

#### Figure 1  ####
library(ggplot2)
twitter.age <- ggplot(data = bes[!is.na(bes$twitterUse), ], 
                      aes(x = Age, group = Group, 
                          fill = Group,
                          colour = Group, 
                          weights = wt_combined_main)) +
  geom_density(alpha = 0.2) + theme_bw()

bes$Group <- bes$fbUse
fb.age <- ggplot(data = bes[!is.na(bes$twitterUse), ], aes(x = Age, group = Group, 
                                                 fill = Group,
                                                 colour = Group, 
                                                 weights = wt_combined_main)) +
  geom_density(alpha = 0.2) + theme_bw()

library(gridExtra)

grid.arrange(fb.age, twitter.age, ncol = 2)


xBySoc <- function(xvar, socMedia, formula) {
  library(survey)
  bes.design <- svydesign(id = bes$finalserialno, 
                          weights = bes$wt_combined_main,
                          data = bes)
  ed.sm <- prop.table(svytable(design = bes.design, 
                                    formula = formula), 1)
  
  library(reshape)
  ed.sm <- melt(ed.sm)
  colnames(ed.sm) <- c("Group", "xvar", "prop")
  ed.sm$xvar <- factor(ed.sm$xvar, levels = unique(ed.sm$xvar))
  ed.sm$Group <- factor(ed.sm$Group, levels = unique(ed.sm$Group))
  
  bar.plot <- ggplot(ed.sm, aes(x = xvar, y = prop, group = Group, fill = Group)) + 
    geom_bar(stat = "identity", position = "dodge", colour = "black", width = 0.75) + theme_bw()+
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) + ylab("Proportion")
  return(bar.plot)
}

#### Figure 2 ####
gender.twitter <- xBySoc(xvar = "gender", socMedia = "twitterUse",
                         formula = ~twitterUse + gender) + xlab("Gender")
gender.fb <- xBySoc(xvar = "gender", socMedia = "fbUse",
                    formula = ~fbUse + gender) + xlab("Gender")
grid.arrange(gender.fb, gender.twitter)


#### Figure 3 ####
ed.twitter <- xBySoc(xvar = "edlevel", socMedia = "twitterUse", 
       formula = ~twitterUse + edlevel) + xlab("Education")
ed.fb <- xBySoc(xvar = "edlevel", socMedia = "fbUse", 
       formula = ~fbUse + edlevel) + xlab("Education")

grid.arrange(ed.fb, ed.twitter)


ed.gender.socmedia <- arrangeGrob(gender.fb + theme(legend.position = "none")  +
               ggtitle("Facebook/Gender") + xlab("") + ylim(0, 0.6)+ theme(panel.grid.major.x = element_blank()), 
               gender.twitter + theme(legend.position = "none") + 
                 ggtitle("Twitter/gender")  + xlab("")+ ylim(0, 0.6)+ theme(panel.grid.major.x = element_blank()),
             ed.fb + theme(legend.position = "none")  + 
               ggtitle("Facebook/education")  + xlab("") + ylim(0, 0.4)+ theme(panel.grid.major.x = element_blank()), 
              ed.twitter + theme(legend.position = "none") + 
               ggtitle("Twitter/education")  + xlab("")+ ylim(0, 0.4)+ theme(panel.grid.major.x = element_blank()), ncol= 2)

ggsave(ed.gender.socmedia, file = "edsocmedia.png", dpi = 700, width =10)
ggsave(ed.gender.socmedia, file = "edsocmedia.eps", dpi = 700, width =10)


legend <- ggplot(dtf(Group = factor(c("Population", "User", "Non-user"), 
                                     levels = c("Population", "User", "Non-user")),x=1), 
       aes(fill = Group,x=x)) + geom_bar(colour = "black") + 
  theme(legend.position = "bottom",legend.box.margin = margin(15,15,15,15)) 
ggsave(legend, file = "legend.png", dpi = 700)


